package com.kalaqia.base.ui;

import android.app.Activity;
import android.graphics.Color;
import android.os.Build;
import android.support.design.widget.CoordinatorLayout;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import java.lang.reflect.Field;
import java.lang.reflect.Method;


/**
 * @author kalaqia 421407393@qq.com
 * @since 0.0.5
 */
public class KaStatusBarUtil {

    /**
     * 设置状态栏及字体颜色 需要api23
     *
     * @param activity        需要设置的activity
     * @param color           状态栏颜色
     * @param isFontColorDark 字体是否深色
     */
    public static void setWindowStatusBarColor(Activity activity, int color, boolean isFontColorDark
            , boolean isDraw) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            try {
                Window window = activity.getWindow();
                if (isDraw) {
                    window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                }
                setStatusBarFontColorDark(window, isFontColorDark);
                window.setStatusBarColor(color);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static void setStatusBarFontColorDark(Activity activity, boolean isFontColorDark) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
            return;
        }
        Window window = activity.getWindow();
        try {
            if (isFontColorDark) {
                window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
            } else {
                window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
            }
            //
            if ("Xiaomi".equals(Build.MANUFACTURER)) {
                MIUISetStatusBarFontColorDark(window, isFontColorDark);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 设置状态栏字体图标为深色
     *
     * @param window          需要设置的窗口
     * @param isFontColorDark 是否把状态栏字体及图标颜色设置为深色
     */
    private static void setStatusBarFontColorDark(Window window, boolean isFontColorDark) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            try {
                if (isFontColorDark) {
                    window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
                } else {
                    window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
                }
                //
                if ("Xiaomi".equals(Build.MANUFACTURER)) {
                    MIUISetStatusBarFontColorDark(window, isFontColorDark);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


    /**
     * MIUI设置状态栏字体图标为深色，需要MIUI6以上
     *
     * @param window          需要设置的窗口
     * @param isFontColorDark 是否把状态栏字体及图标颜色设置为深色
     * @return boolean 成功执行返回true
     */
    private static boolean MIUISetStatusBarFontColorDark(Window window, boolean isFontColorDark) {
        boolean result = false;
        if (window != null) {
            Class clazz = window.getClass();
            try {
                int darkModeFlag;
                Class layoutParams = Class.forName("android.view.MiuiWindowManager$LayoutParams");
                Field field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE");
                darkModeFlag = field.getInt(layoutParams);
                Method extraFlagField = clazz.getMethod("setExtraFlags", int.class, int.class);
                if (isFontColorDark) {
                    extraFlagField.invoke(window, darkModeFlag, darkModeFlag);
                } else {
                    extraFlagField.invoke(window, 0, darkModeFlag);
                }
                result = true;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    public static void setColorForSwipeBack(BaseActivity activity, boolean b) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            ViewGroup contentView = activity.findViewById(android.R.id.content);
            View rootView = contentView.getChildAt(0);
//            int statusBarHeight = getStatusBarHeight(activity);
            rootView.setFitsSystemWindows(true);
//            contentView.setPadding(0, statusBarHeight, 0, 0);
//            contentView.setBackgroundColor(calculateStatusColor(color, statusBarAlpha));
            setTransparentForWindow(activity);
        }
    }


    /**
     * 设置透明
     */
    private static void setTransparentForWindow(Activity activity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            activity.getWindow().setStatusBarColor(Color.TRANSPARENT);
            activity.getWindow()
                    .getDecorView()
                    .setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            activity.getWindow()
                    .setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
    }
}
